<?php

// Should this be moved to the database?
$lastfm_baseurl = "http://ws.audioscrobbler.com/2.0/";
$lastfm_apikey  = "ce46b5fceb51f22b0198597185cf03df"; 


/*
function GetDataFromURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);    
		return $output;
}
*/

function LastFMUserRequest($username) {
	global $lastfm_baseurl;
	global $lastfm_apikey;
	return "$lastfm_baseurl?api_key=$lastfm_apikey&user=$username";
}

function LastFMTopTracks($username) {
	global $SITE_MIDDLE;
	global $lang;
	$requesturl = LastFMUserRequest($username) . "&method=user.getTopTracks";
	$xml = GetDataFromURL($requesturl);
	$sxml = new SimpleXMLElement($xml);
	$i=0;
	if ($sxml['status']=="ok") {

		$tracks = "<table width='100%'>";
		
		foreach ($sxml->toptracks [0]->track as $track){
			$i++;
			if ($i>10) break;
			$tracks .= "<tr><td width='50%'>".$track->artist->name."</td>";
			$tracks .= "<td width='50%'>$track->name</td></tr>";
		}
		$tracks.= "</table>";
	} else {
		$tracks = GetLangString($lang,"msg_externalerror");
	}
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_lastfmtoptracks"), $tracks);	
}

function LastFMWeeklyTrackChart($username) {
	global $SITE_MIDDLE;
	global $lang;
	$requesturl = LastFMUserRequest($username) . "&method=user.getWeeklyTrackChart";
	$xml = GetDataFromURL($requesturl);
	$sxml = new SimpleXMLElement($xml);
	if ($sxml['status']=="ok") {
		$tracks = "<table width='100%'>";
		$i=0;
		foreach ($sxml->weeklytrackchart [0]->track as $track){
			$i++;
			if ($i>10) break;
			$tracks .= "<tr><td width='50%'>$track->artist</td>";
			$tracks .= "<td width='50%'>$track->name</td></tr>";
		}
		$tracks.= "</table>";
	} else {
		$tracks = GetLangString($lang,"msg_externalerror");
	}
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_lastfmweeklytrackchart"), $tracks);	
}

function LastFMRecentTracks($username) {
	global $SITE_MIDDLE;
	global $lang;
	$requesturl = LastFMUserRequest($username) . "&method=user.getRecentTracks";
	$xml = GetDataFromURL($requesturl);
	$sxml = new SimpleXMLElement($xml);
	if ($sxml['status']=="ok") {
		$tracks = "<table width='100%'>";
		foreach ($sxml->recenttracks[0]->track as $track){
			$tracks .= "<tr><td width='50%'>$track->artist</td>";
			$tracks .= "<td width='50%'>$track->name</td></tr>";
		}
		$tracks.= "</table>";
	} else {
		$tracks = GetLangString($lang,"msg_externalerror");
	}
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_lastfmrecenttracks"), $tracks);
}

$query = "select * from `profileexternal` where `user`='$profile_id'";
$result = mysql_query($query);
if ($result) {
	$external = mysql_fetch_array($result);
	$query = "select * from `external` where `site`='last.fm' and `userid`='$profile_id'";
	$result = mysql_query($query);
	if ($result) {
		$lastfm = mysql_fetch_array($result);
		$lastfmname = $lastfm['username'];
		if ($external['lastfm'] == 1) LastFMTopTracks($lastfmname);
		if ($external['lastfm'] == 2) LastFMWeeklyTrackChart($lastfmname);
		if ($external['lastfm'] == 3) LastFMRecentTracks($lastfmname);
	}
}
?>